-- UIEarthPortalEffect
-- Created by zouyb
-- 大地之门特效界面

UIEarthPortalEffect = class("UIEarthPortalEffect", function()
    return cc.Layer:create();
end);

function UIEarthPortalEffect.create(classId, callback)
    return UIEarthPortalEffect.new(classId, callback);
end

-- 构造函数
function UIEarthPortalEffect:ctor(classId, callback)
    self:setName("UIEarthPortalEffect");
    local node = cc.CSLoader:createNode("layout/spell/EarthPortalEffect.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 2);

    self.classId = classId;
	self.callback = callback;

    self:resize();

	-- 播放特效
	self:play();
end

-- 适配
function UIEarthPortalEffect:resize()
    local node = self.node;
    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");
    
    -- mask铺满屏幕
    AlignM.fitToScreen(node, "mask");    
end

-- 播放特效
function UIEarthPortalEffect:play()
	-- 播放门动画
	local gateParent = findChildByName(self.node, "CT/panel/gate");
	local lightParent = findChildByName(self.node, "CT/light");

    local mask = findChildByName(self.node, "mask");
    mask:setOpacity(0); 
    mask:setVisible(false);

	playEffect(gateParent, 1284, 320, 0);
	playEffect(lightParent, 1285, 0, 0);

    local uiLevel = UIDungeonMgr.getCurLevel();
    if uiLevel then
        playShakeEffect(uiLevel, 0.03, 4, 8);
        
        performWithDelay(uiLevel, function() 
            playShakeEffect(uiLevel, 0.02, 8, 40);
            playShakeEffect(self, 0.02, 8, 40); 
        end, 1);
    end
    
	local function onFinish()
        local callfunc = cc.CallFunc:create(function()	   
    		if self.callback then
                self.callback(self.classId);
    		end
    
    		UIMgr.getCurrentScene():removeFormByName("UIEarthPortalEffect");
    	end);
    	local fadeIn = cc.FadeIn:create(1.5);
        mask:setVisible(true);
        mask:runAction(cc.Sequence:create(fadeIn, callfunc));
	end

	-- 等待动画播放完毕，中间玩家无法进行其他操作
	wait(3, onFinish);
end



